package com.nd.teamfile.fileexplorer.helper;

import java.util.HashMap;

import android.content.Context;
import android.widget.ImageView;

import com.nd.teamfile.fileexplorer.R;
import com.nd.teamfile.fileexplorer.helper.FileCategoryHelper.FileCategory;
import com.nd.teamfile.fileexplorer.sdk.type.FileExplorerInfo;
import com.nd.teamfile.fileexplorer.util.FileExplorerUtil;

/**
 * 文件图标管理类
 * 
 * @ClassName FileIconHelper
 * @Function TODO ADD FUNCTION
 * @Reason TODO ADD REASON
 * 
 * @author HuangYK
 * @version v1.3.3
 * @Date 2014-1-24 下午3:43:20
 * 
 * @see
 */
public class FileIconHelper{

//	private static HashMap<ImageView, ImageView> imageFrames = new HashMap<ImageView, ImageView>();

	private static HashMap<String, Integer> fileExtToIcons = new HashMap<String, Integer>();

	private FileIconLoader mIconLoader;

	static {
		// 音频
		addItem(new String[] { "MP3", "WMA", "WAV", "MID" },
				R.drawable.file_icon_audio);
		// 视频
		addItem(new String[] { "MP4", "WMV", "MPEG", "M4V", "3GP", "3GPP",
				"3g2", "3gpp2", "asf" }, R.drawable.file_icon_video);
		// 图片
		addItem(new String[] { "JPG", "JPEG", "GIF", "PNG", "BMP", "WBMP" },
				R.drawable.file_icon_picture);
		// 文本文件
		addItem(new String[] { "TXT", "LOG", "XML", "INI", "LRC" },
				R.drawable.file_icon_txt);
		// word
		addItem(new String[] { "DOC", "DOT", "DOCX", "DOTX" },
				R.drawable.file_icon_word);
		// excel
		addItem(new String[] { "XLS", "XLT", "XLSX", "XLTX" },
				R.drawable.file_icon_excel);
		// PPT
		addItem(new String[] { "PPT", "POT", "PPS", "PPTX", "POTX", "PPSX", },
				R.drawable.file_icon_pdf);
		// PDF
		addItem(new String[] { "PDF" }, R.drawable.file_icon_pdf);
		// 压缩文件
		addItem(new String[] { "ZIP", "RAR" }, R.drawable.file_icon_zip);
		// PSD
		addItem(new String[] { "PSD" }, R.drawable.file_icon_psd);
	}

	public FileIconHelper(Context context) {
		mIconLoader = new FileIconLoader(context);
	}

	private static void addItem(String[] exts, int resId) {
		if (exts != null) {
			for (String ext : exts) {
				fileExtToIcons.put(ext.toLowerCase(), resId);
			}
		}
	}

	/**
	 * 根据扩展名获取文件图标
	 * 
	 * @n<b>函数名称</b> :getFileIcon
	 * @brief
	 * @see
	 * @version Ver 1.3.3
	 * @param @param ext
	 * @param @return
	 * @return int
	 * @<b>作者</b> : HuangYK
	 * @<b>创建时间</b> : 2014-1-26下午1:49:42
	 */
	public static int getFileIcon(String ext) {
		Integer i = fileExtToIcons.get(ext.toLowerCase());
		if (i != null) {
			return i.intValue();
		} else {
			return R.drawable.file_icon_default;
		}

	}

	/**
	 * 设置文件图标
	 * 
	 * @n<b>函数名称</b> :setIcon
	 * @brief
	 * @see
	 * @version Ver 1.3.3
	 * @param @param fileInfo
	 * @param @param fileImage
	 * @param @param fileImageFrame
	 * @return void
	 * @<b>作者</b> : HuangYK
	 * @<b>创建时间</b> : 2014-1-24下午4:18:16
	 */
	public void setIcon(FileExplorerInfo fileInfo, ImageView ivFileIcon){//,
//			ImageView ivFileIconFrame) {
		String filePath = fileInfo.filePath;
		long fileId = fileInfo.dbId;

		// 先根据文件路径获取文件后缀来设置相应的默认图标
		String extFromFilename = FileExplorerUtil.getExtFromFilename(filePath)
				.toUpperCase();
		int id = getFileIcon(extFromFilename);
		ivFileIcon.setImageResource(id);

		// 通过文件路径获取文件类别
		FileCategory fc = FileCategoryHelper.getCategoryFromPath(filePath);
//		ivFileIconFrame.setVisibility(View.GONE);
		boolean set = false;
		// 先取消请求
		mIconLoader.cancelRequest(ivFileIcon);

		// 如果有获取到以下文件类型，则尝试去获取对应文件的缩略图
		switch (fc) {
		case Apk:
			set = mIconLoader.loadIcon(ivFileIcon, filePath, fileId, fc);
			break;
		case Picture:
		case Video:
			set = mIconLoader.loadIcon(ivFileIcon, filePath, fileId, fc);
			if (set){
//				ivFileIconFrame.setVisibility(View.VISIBLE);
			}else {
				ivFileIcon
						.setImageResource(fc == FileCategory.Picture ? R.drawable.file_icon_picture
								: R.drawable.file_icon_video);
//				ivFileIconFrame.setVisibility(View.GONE);
//				imageFrames.put(ivFileIcon, ivFileIconFrame);
			}
			break;
		default:
			// 除了已case的三种类型从系统去获取icon，其他类型不处理
			break;
		}
	}
}
